﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;

namespace ProjectBase.Core
{
    /// <summary>
    ///     Allows making all static caches to be dependent on a shared file - last write datetime.
    ///     (if allowed in Project.config)
    ///     This single point grants common and simple static caches cleanup,
    ///     only by updating file's last write datetime.
    ///     Also working for webfarms or multi worker process.
    /// </summary>
    public class FileDependency : IPublishingReporter
    {
        #region members
        string _fileName;
        DateTime _lastWriteTime;
        #endregion members

        #region constructor
        public FileDependency(string fileName)
        {
            if (fileName.IsEmpty())
            {
                return;
            }

            var fullName = Global.CreateFilePath(fileName);
            if (!File.Exists(fullName))
            {
                return;
            }

            _fileName = fullName;
            _lastWriteTime = File.GetLastWriteTime(_fileName);
        }
        #endregion constructor

        #region public
        public virtual bool HasChanged
        {
            get
            {
                if (!Global.Settings.AllowFileCacheDependencyForStatic)
                {
                    return false;
                }
                if (FileName.IsEmpty()
                    || !File.Exists(FileName))
                {
                    return false;
                }
                if (File.GetLastWriteTime(FileName).Equals(LastWriteTime))
                {
                    return false;
                }
                this.PublishDebug("FileCache for static field has changed");
                LastWriteTime = File.GetLastWriteTime(FileName);
                return true;
            }
        }

        public virtual void Invalidate()
        {
            try
            {
                if (!Global.Settings.AllowFileCacheDependencyForStatic)
                {
                    return;
                }
                if (FileName.IsEmpty()
                    || !File.Exists(FileName))
                {
                    return;
                }

                this.PublishDebug("FileCache for static field was invalidated ");

                File.SetLastWriteTime(FileName, DateTime.Now);
            }
            catch (Exception ex)
            {
                this.PublishFatal("Cannot change cache file", ex);
                throw;
            }
        }
        #endregion public

        #region protected
        protected virtual DateTime LastWriteTime
        {
            get { return _lastWriteTime; }
            set { _lastWriteTime = value; }
        }

        protected virtual string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }
        #endregion protected
    }
}